package com.goldenSubmarine.ang.rotator 
{
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.loadingtypes.LoadingItem;
	import com.nuke.text.FontTextField;
	import com.nuke.utils.Library;
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.setTimeout;
	import msz.utils.MSZUtils;
	/**
	 * ...
	 * @author Bartłomiej JERZY
	 */
	public class Photo extends Sprite
	{
		public static var WIDTH  					:int = 314
		public static var HEIGHT  					:int = 406
		public static var PHOTO_HEIGHT  			:int = 397
		
		private var _name							:FontTextField = new FontTextField({font:"HelveticaNeue",size:24,color:0x7a003a,bold:true},{autoSize:"left"})
		private var _content						:FontTextField = new FontTextField({font:"HelveticaNeue",size:16,color:0x7a003a},{autoSize:"left",width:WIDTH - 10,wordWrap:true,multiline:true})
		private var _bmp							:Bitmap
		private var _loader							:BulkLoader = BulkLoader.getLoader("main")
		
		private var _cont							:Sprite = new Sprite()
		private var _mask							:Sprite = new Sprite()
		private var _data							:Object;
		
		private var _height							:Number					
		
		private var _opened							:Number = 0
		private var _pre							:MovieClip = Library.getMovieClip("preCursor")
		private var _loaded							:Boolean = false
		
		public function Photo($data:Object) 
		{
			_data = $data
			init()
			loadPhoto()
		}
		
		private function loadPhoto():void {
			var _li:LoadingItem = _loader.add(String(_data.img[0]))
			
			if (_li.isLoaded) {
				photoLoaded(null)
			}else {
				_pre.x = WIDTH/2
				_pre.y = PHOTO_HEIGHT / 2
				_pre.alpha = 0.5
				addChild(_pre)
				_li.addEventListener(Event.COMPLETE,photoLoaded)
			}
		}
		
		private function photoLoaded(e:Event):void {
			if(contains(_pre)) removeChild(_pre)
			_bmp = new Bitmap(_loader.getBitmapData(String(_data.img[0])))
			_cont.addChild(_bmp)
			
			_loaded = true
			setTimeout(dispatchEvent,150,new Event(Event.COMPLETE))
		}
		
		private function init():void
		{
			_name.htmlText = _data.name[0]
			_name.y = PHOTO_HEIGHT + 5
			_name.x =  5
			
			_content.htmlText = _data.content[0]
			_content.y = _name.y+_name.height+10
			_content.x =  5
			
			_cont.addChild(_name)
			_cont.addChild(_content)
			
			_cont.graphics.beginFill(0xffffff)
			_cont.graphics.drawRect(0,0,WIDTH,_content.y+_content.height+10)
			_cont.graphics.endFill()
			
			_cont.graphics.beginFill(0x7a003a,0.2)
			_cont.graphics.drawRect(5,_name.y+_name.height+5,WIDTH-10,1)
			_cont.graphics.endFill()
			
			_mask.graphics.beginFill(0)
			_mask.graphics.drawRect(0,0,WIDTH,_name.y+_name.height+3)
			_mask.graphics.endFill()
			
			_cont.mask = _mask
			
			addChild(_cont)
			addChild(_mask)
		}
		
		override public function get height():Number { return _mask.height; }
		
		override public function set height(value:Number):void 
		{
			_height = value;
		}
		
		public function get opened():Number { return _opened; }
		
		public function set opened(value:Number):void 
		{
			_opened = value;
			
			_mask.height = _name.y+_name.height+3 +_opened*(_content.y+_content.height+10-_name.y-_name.height-5)
		}
		
		public function get loaded():Boolean { return _loaded; }
		
		
	}

}